package com.nexgo.payment.pack;

import com.nexgo.common.ByteUtils;
import com.nexgo.data.entity.SendData;
import com.nexgo.data.entity.SendDataBuilder;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.util.MessageHelper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 非接参数下载
 *
 * Created by xiaox on 2017/8/14.
 */
class RfParamDownBuilder extends DataBuilder {
    RfParamDownBuilder(TransData transData, DataSource repository) {
        super(transData, repository);
    }

    @Override
    SendData form() {
        SendData sendData = new SendDataBuilder().setListener(mListener).setSignListener(mSignListener).createSendData();
        sendData.setMsgType(mTransData.getTransType().getMsgType());
        sendData.setHeader(MessageHelper.formMessageHeader(mRepository.getParamValue(PreferencesConstants.TPDU)));
//        sendData.setField60(formField60());
        //        wzp add
        sendData.setProcCode(mTransData.getTransType().getProcCode());
        sendData.setTraceNum(mRepository.getParamValue(PreferencesConstants.TRACE_NUMBER));
        mRepository.increaseTraceNumber();
//        12
        String date = new SimpleDateFormat("yyyyMMdd", Locale.getDefault()).format(new Date());
        String time = new SimpleDateFormat("HHmmss", Locale.getDefault()).format(new Date());
        sendData.setTime(time);
//        13
        sendData.setDate(date.substring(4,8));
//        35 下载偏移
        sendData.setTrack2(mTransData.getTemporary());
        sendData.setTerminalId(mRepository.getParamValue(PreferencesConstants.TERMINAL_ID));
        sendData.setMerchantId(mRepository.getParamValue(PreferencesConstants.MERCHANT_ID));
        sendData.setMac("0000000000000000");
        return sendData;
    }


    private String formField60() {
        StringBuilder sb = new StringBuilder();
        sb.append(mTransData.getTransType().getFuncCode());
        sb.append(mRepository.getParamValue(PreferencesConstants.BATCH_NUMBER));
        sb.append("394");
        return ByteUtils.byteArray2HexString(sb.toString().getBytes());
    }
}
